package com.lyb.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lyb.system.domain.Employee;
import com.lyb.system.domain.Tenant;
import com.lyb.system.dto.EnteringDTO;
import com.lyb.system.mapper.EmployeeMapper;
import com.lyb.system.mapper.MealMapper;
import com.lyb.system.mapper.TenantMapper;
import com.lyb.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyb
 * @since 2020-02-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MealMapper mealMapper;

    @Override
    public void entering(EnteringDTO enteringDTO) {
        // 保存租户
        Date date = new Date();
        // 获取租户信息
        Tenant tenant = enteringDTO.getTenant();
        // 实时的时间
        tenant.setRegisterTime(date);
        baseMapper.insert(tenant);

        // 管理员账户
        Employee employee = enteringDTO.getEmployee();
        // 入住时间
        employee.setInputTime(date);
        // 员工状态
        employee.setState(Employee.STATE_NORMAL);
        // 机构id
        employee.setTenantId(tenant.getId());
        // 用户类型
        employee.setType(Employee.TYPE_ADMIN);
        employeeMapper.insert(employee);

        // 租户和套餐的中间表                     套餐ID，                  机构ID ，      状态（是否支付、是否过期）
        mealMapper.insertRelationMealAndTenant(enteringDTO.getMealId() , tenant.getId(),0);
    }
}
